package com.elite.groovy.callback

import com.elite.groovy.davco.clink.ClinkUtils
import com.elite.groovy.kh.KHUtil
import com.elite.groovy.kh.LogToDB
import com.elite.groovy.util.EliteBean
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat

/**
 * @projectName: Groovy
 * @package: com.elite.groovy.callback
 * @className: apiClinkCdrRecord
 * @description: 同步天润IB呼入&OB呼出记录数据
 * @author: LiJT
 * @date: 2023-10-27 11:13
 * @version: 1.0
 */

public class apiClinkCdrCopyRecord implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiClinkCdrCopyRecord.class);

    private static Map<String, String> headMap = new HashMap<String, String>();
    private static SimpleDateFormat sdf_Oper_No = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private static String Oper_No = "";

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        // 封装调用头Heads
        headMap.put("Content-Type", "application/json");

        try {
            // 获取数据源
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

            // 创建日期格式对象并设置要输出的格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date date = new Date();

            String StartTime = request.getParameter("StartTime");
            log.debug("doCallBack API StartTime:" + StartTime);

            StartTime = KHUtil.isNull(StartTime) ? sdf.format(date) : StartTime;
            log.debug("同步天润IB呼入&OB呼出记录数据 StartTime:" + StartTime);

            Oper_No = StartTime + "_" + sdf_Oper_No.format(date);
            log.debug("同步天润IB呼入&OB呼出记录数据 Oper_No:" + Oper_No);

            // 同步IB呼入数据
            log.debug("1-同步IB呼入数据...Result:" + toDoIBGet(eliteBean, jt, StartTime, ""));

            // 同步IB呼入数据-明细
            log.debug("2-同步IB呼入数据-明细...Result:" + toDoIBGetd(eliteBean, jt, StartTime, ""));

            // 同步OB呼出数据
            log.debug("3-同步OB呼出数据...Result:" + toDoOBGet(eliteBean, jt, StartTime, ""));

            // 同步OB呼出数据-明细
            log.debug("4-同步OB呼出数据-明细...Result:" + toDoOBGetd(eliteBean, jt, StartTime, ""));

            log.debug("通话数据同步任务结束执行...");
        } catch (Exception e) {
            log.error("HTTP请求调用错误，接口处理异常：" + e.getMessage(), e);
            return e.getMessage();

        }

        return "SUCC";

    }

    // 1-呼入通话记录
    static String toDoIBGet(EliteBean eliteBean, JdbcTemplate jt, String StartTime, String Offset) {

        //处理主数据
        log.debug("1-呼入通话记录 toDoGet Data StartTime:" + StartTime + " Offset:" + Offset);

        long startTime_Loop = System.currentTimeMillis();

        // 系统接入-GET
        String getPath = "/cc/copy_cdr_ibs?";

        int totalNum = 0;
        int loopLimit = 0;

        List<JSONObject> ibslist = new ArrayList<JSONObject>();
        while (!Offset.equals("noScrollId") && loopLimit < 1000) {
            Map<String, String> queryParams = new HashMap<String, String>();
            queryParams.put("hiddenType", "0");
            queryParams.put("date", StartTime);

            if (!KHUtil.isNull(Offset)) {
                queryParams.put("scrollId", Offset);
            }
            queryParams.put("limit", "100");
            log.debug("系统接入 queryParams:" + queryParams);

            String doGETRes = ClinkUtils.doClinkCall("GET", getPath, queryParams, headMap, null);
            log.debug("系统接入 doGETRes:" + doGETRes);

            JSONObject doGETJSON = JSONObject.fromObject(doGETRes);
            // log.debug("1-呼入通话记录 Data Result:" + doGETJSON);

            JSONArray dataArray = doGETJSON.getJSONArray("cdrIbs"); ;
            int dataArrayLen = dataArray.size();
            totalNum = totalNum + dataArrayLen;
            log.info("1-呼入通话记录 Data Result size:" + dataArrayLen);


            for (int i = 0; i < dataArray.size(); i++) {
                ibslist.add(dataArray.getJSONObject(i));
            }


            loopLimit++;

            Offset = KHUtil.GetSFJV(doGETJSON, "scrollId");
            Offset = KHUtil.isNull(Offset) ? "noScrollId" : Offset;

        }

        // 先将所有数据按开始时间排序
        Collections.sort(ibslist, new Comparator<JSONObject>() {
            @Override
            public int compare(JSONObject o1, JSONObject o2) {
                String startTime1 = o1.getString("startTime");
                String startTime2 = o2.getString("startTime");
                return startTime1.compareTo(startTime2);
            }
        });

        // 将所有数据写入表
        int bslist_Size = ibslist.size();
        log.debug("1-呼入通话记录 所有数据写入表数量:" + bslist_Size);
        for (int i = 0; i < bslist_Size; i++) {
            JSONObject dataObject = ibslist.get(i);
            String uniqueId = KHUtil.GetSFJV(dataObject, "uniqueId");
            String hotline = KHUtil.GetSFJV(dataObject, "hotline");
            String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
            String customerNumberEncrypt = KHUtil.GetSFJV(dataObject, "customerNumberEncrypt");
            String customerProvince = KHUtil.GetSFJV(dataObject, "customerProvince");
            String customerCity = KHUtil.GetSFJV(dataObject, "customerCity");
            String qno = KHUtil.GetSFJV(dataObject, "qno");
            String qname = KHUtil.GetSFJV(dataObject, "qname");
            String cno = KHUtil.GetSFJV(dataObject, "cno");
            String clientName = KHUtil.GetSFJV(dataObject, "clientName");
            String clientNumber = KHUtil.GetSFJV(dataObject, "clientNumber");
            String clientRingingTime = KHUtil.GetSFJV(dataObject, "clientRingingTime");
            String callType = KHUtil.GetSFJV(dataObject, "callType");
            String startTime = KHUtil.GetSFJV(dataObject, "startTime");
            String endTime = KHUtil.GetSFJV(dataObject, "endTime");
            String bridgeTime = KHUtil.GetSFJV(dataObject, "bridgeTime");
            String bridgeDuration = KHUtil.GetSFJV(dataObject, "bridgeDuration");
            String totalDuration = KHUtil.GetSFJV(dataObject, "totalDuration");
            String ivrName = KHUtil.GetSFJV(dataObject, "ivrName");
            String status = KHUtil.GetSFJV(dataObject, "status");
            String statusCode = KHUtil.GetSFJV(dataObject, "statusCode");
            String endReason = KHUtil.GetSFJV(dataObject, "endReason");
            String sipCause = KHUtil.GetSFJV(dataObject, "sipCause");
            String recordFile = KHUtil.GetSFJV(dataObject, "recordFile");
            String userField = KHUtil.GetSFJV(dataObject, "userField");
            String mark = KHUtil.GetSFJV(dataObject, "mark");
            String tags = KHUtil.GetSFJV(dataObject, "tags");
            String markData = KHUtil.GetSFJV(dataObject, "markData");
            String joinQueueTime = KHUtil.GetSFJV(dataObject, "joinQueueTime");
            String leaveQueueTime = KHUtil.GetSFJV(dataObject, "leaveQueueTime");
            String ibWaitDuration = KHUtil.GetSFJV(dataObject, "ibWaitDuration");
            String sayVoiceDuration = KHUtil.GetSFJV(dataObject, "sayVoiceDuration");
            String clientOffhookTime = KHUtil.GetSFJV(dataObject, "clientOffhookTime");
            String ibRingingDuration = KHUtil.GetSFJV(dataObject, "ibRingingDuration");
            String queueAnswerInTime = KHUtil.GetSFJV(dataObject, "queueAnswerInTime");
            String evaluation = KHUtil.GetSFJV(dataObject, "evaluation");
            String hotlineName = KHUtil.GetSFJV(dataObject, "hotlineName");
            String firstJoinQueueTime = KHUtil.GetSFJV(dataObject, "firstJoinQueueTime");
            String callId = KHUtil.GetSFJV(dataObject, "callId");

            String sql = '''Replace Into Clink_List_Cdr_Ibs
                      (Uniqueid,
                       Hotline,
                       Customernumber,
                       Customernumberencrypt,
                       Customerprovince,
                       Customercity,
                       Qno,
                       Qname,
                       Cno,
                       Clientname,
                       Clientnumber,
                       Clientringingtime,
                       Calltype,
                       Starttime,
                       Endtime,
                       Bridgetime,
                       Bridgeduration,
                       Totalduration,
                       Ivrname,
                       Status,
                       Statuscode,
                       Endreason,
                       Sipcause,
                       Recordfile,
                       Userfield,
                       Mark,
                       Tags,
                       Markdata,
                       Joinqueuetime,
                       Leavequeuetime,
                       Ibwaitduration,
                       Sayvoiceduration,
                       Clientoffhooktime,
                       Ibringingduration,
                       Queueanswerintime,
                       Evaluation,
                       Hotlinename,
                       Firstjoinqueuetime,
                       Callid)
                    Values
                      (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''';

            jt.update(sql, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, qno, qname, cno, clientName, clientNumber, clientRingingTime, callType, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, mark, tags, markData, joinQueueTime, leaveQueueTime, ibWaitDuration, sayVoiceDuration, clientOffhookTime, ibRingingDuration, queueAnswerInTime, evaluation, hotlineName, firstJoinQueueTime, callId);

            callRepeatFix(jt, uniqueId);
        }


        LogToDB.toSyncFile_Log(eliteBean, Oper_No + KHUtil.getIntRandom(4), "1-呼入通话记录", "apiClinkCdrCopyRecord", "toDoIBGet", getPath, String.valueOf(loopLimit), String.valueOf(totalNum), "获取次数:" + loopLimit + " 总数量:" + totalNum, String.valueOf(System.currentTimeMillis() - startTime_Loop));

        return "获取次数:" + loopLimit + " 总数量:" + totalNum;

    }

    // 2-呼入通话记录明细
    static String toDoIBGetd(EliteBean eliteBean, JdbcTemplate jt, String StartTime, String Offset) {

        //处理主数据
        log.debug("2-呼入通话记录明细 toDoGet Data StartTime:" + StartTime + " Offset:" + Offset);

        long startTime_Loop = System.currentTimeMillis();

        // 系统接入-GET
        String getPath = "/cc/copy_cdr_ibs?";

        int totalNum = 0;
        int loopLimit = 0;
        while (!Offset.equals("noScrollId") && loopLimit < 1000) {
            Map<String, String> queryParams = new HashMap<String, String>();
            queryParams.put("hiddenType", "0");
            queryParams.put("date", StartTime);

            if (!KHUtil.isNull(Offset)) {
                queryParams.put("scrollId", Offset);
            }
            queryParams.put("limit", "100");
            log.debug("系统接入 queryParams:" + queryParams);

            String doGETRes = ClinkUtils.doClinkCall("GET", getPath, queryParams, headMap, null);
            log.debug("系统接入 doGETRes:" + doGETRes);

            JSONObject doGETJSON = JSONObject.fromObject(doGETRes);
            // log.debug("2-呼入通话记录明细 Data Result:" + doGETJSON);

            JSONArray dataArray = doGETJSON.getJSONArray("cdrIbs"); ;
            int dataArrayLen = dataArray.size();
            totalNum = totalNum + dataArrayLen;
            log.info("2-呼入通话记录明细 Data Result size:" + dataArrayLen);

            JSONObject dataObject = null;
            String sql = null;
            List sqlResult = null;
            int Countnum = 0;
            for (int i = 0; i < dataArrayLen; i++) {
                dataObject = dataArray.getJSONObject(i);

                String uniqueId = KHUtil.GetSFJV(dataObject, "uniqueId");
                String callId = KHUtil.GetSFJV(dataObject, "callId");
                String hotline = KHUtil.GetSFJV(dataObject, "hotline");
                String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
                String customerNumberEncrypt = KHUtil.GetSFJV(dataObject, "customerNumberEncrypt");
                String customerProvince = KHUtil.GetSFJV(dataObject, "customerProvince");
                String customerCity = KHUtil.GetSFJV(dataObject, "customerCity");
                String qno = KHUtil.GetSFJV(dataObject, "qno");
                String cno = KHUtil.GetSFJV(dataObject, "cno");
                String clientName = KHUtil.GetSFJV(dataObject, "clientName");
                String clientNumber = KHUtil.GetSFJV(dataObject, "clientNumber");
                String clientRingingTime = KHUtil.GetSFJV(dataObject, "clientRingingTime");
                String callType = KHUtil.GetSFJV(dataObject, "callType");
                String startTime = KHUtil.GetSFJV(dataObject, "startTime");
                String endTime = KHUtil.GetSFJV(dataObject, "endTime");
                String bridgeTime = KHUtil.GetSFJV(dataObject, "bridgeTime");
                String bridgeDuration = KHUtil.GetSFJV(dataObject, "bridgeDuration");
                String totalDuration = KHUtil.GetSFJV(dataObject, "totalDuration");
                String ivrName = KHUtil.GetSFJV(dataObject, "ivrName");
                String status = KHUtil.GetSFJV(dataObject, "status");
                String statusCode = KHUtil.GetSFJV(dataObject, "statusCode");
                String endReason = KHUtil.GetSFJV(dataObject, "endReason");
                String sipCause = KHUtil.GetSFJV(dataObject, "sipCause");
                String recordFile = KHUtil.GetSFJV(dataObject, "recordFile");
                String userField = KHUtil.GetSFJV(dataObject, "userField");
                String ivrFlows = KHUtil.GetSFJV(dataObject, "ivrFlows");
                String investigation = KHUtil.GetSFJV(dataObject, "investigation");
                String tags = KHUtil.GetSFJV(dataObject, "tags");
                String mark = KHUtil.GetSFJV(dataObject, "mark");
                String qname = KHUtil.GetSFJV(dataObject, "qname");
                String markData = KHUtil.GetSFJV(dataObject, "markData");
                String joinQueueTime = KHUtil.GetSFJV(dataObject, "joinQueueTime");
                String leaveQueueTime = KHUtil.GetSFJV(dataObject, "leaveQueueTime");
                String ibWaitDuration = KHUtil.GetSFJV(dataObject, "ibWaitDuration");
                String fastHangUp = KHUtil.GetSFJV(dataObject, "fastHangUp");
                String clientOffhookTime = KHUtil.GetSFJV(dataObject, "clientOffhookTime");
                String sayVoiceDuration = KHUtil.GetSFJV(dataObject, "sayVoiceDuration");
                String ibRingingDuration = KHUtil.GetSFJV(dataObject, "ibRingingDuration");
                String queueAnswerInTime = KHUtil.GetSFJV(dataObject, "queueAnswerInTime");
                String evaluation = KHUtil.GetSFJV(dataObject, "evaluation");
                String firstJoinQueueTime = KHUtil.GetSFJV(dataObject, "firstJoinQueueTime");
                // log.debug("2-呼入通话记录明细 Deal Data uniqueId:" + uniqueId);

                sql = '''replace Into clink_describe_cdr_ib (uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, qno, qname, cno, clientName, clientNumber, clientRingingTime, callType, joinQueueTime, leaveQueueTime, ibWaitDuration, callId, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, ivrFlows, investigation, tags, mark, fastHangUp, sayVoiceDuration, clientOffhookTime, ibRingingDuration, queueAnswerInTime, evaluation, firstJoinQueueTime, markData) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''';

                jt.update(sql, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, qno, qname, cno, clientName, clientNumber, clientRingingTime, callType, joinQueueTime, leaveQueueTime, ibWaitDuration, callId, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, ivrFlows, investigation, tags, mark, fastHangUp, sayVoiceDuration, clientOffhookTime, ibRingingDuration, queueAnswerInTime, evaluation, firstJoinQueueTime, markData);

                // 处理IVR轨迹数据
                if (!KHUtil.isNull(ivrFlows)) {
                    toDoOBGetIVR(eliteBean, jt, uniqueId, ivrFlows);
                }
            }

            loopLimit++;

            Offset = KHUtil.GetSFJV(doGETJSON, "scrollId");
            Offset = KHUtil.isNull(Offset) ? "noScrollId" : Offset;

        }

        LogToDB.toSyncFile_Log(eliteBean, Oper_No + KHUtil.getIntRandom(4), "2-呼入通话记录明细", "apiClinkCdrCopyRecord", "toDoIBGetd", getPath, String.valueOf(loopLimit), String.valueOf(totalNum), "获取次数:" + loopLimit + " 总数量:" + totalNum, String.valueOf(System.currentTimeMillis() - startTime_Loop));

        return "获取次数:" + loopLimit + " 总数量:" + totalNum;


    }

    // 2-1-呼入通话记录明细-IVR
    static String toDoOBGetIVR(EliteBean eliteBean, JdbcTemplate jt, String uniqueId, String IVRData) {

        //处理主数据
        log.debug("2-1-呼入通话记录明细-IVR toDoGet Data uniqueId:" + uniqueId);

        long startTime_Loop = System.currentTimeMillis();

        JSONArray dataArray = JSONArray.fromObject(IVRData);
        int dataArrayLen = dataArray.size();
        log.debug("2-1-呼入通话记录明细-IVR Data Result size:" + dataArrayLen);

        String sql = "Delete From clink_describe_cdr_ib_ivr Where uniqueId = ?";
        jt.update(sql, uniqueId);

        JSONObject dataObject = null;
        for (int i = 0; i < dataArrayLen; i++) {
            dataObject = dataArray.getJSONObject(i);
            String pathName = KHUtil.GetSFJV(dataObject, "pathName");
            String path = KHUtil.GetSFJV(dataObject, "path");
            String routerStartTime = KHUtil.GetSFJV(dataObject, "routerStartTime");
            String routerEndTime = KHUtil.GetSFJV(dataObject, "routerEndTime");
            String routerName = KHUtil.GetSFJV(dataObject, "routerName");
            String ivrName = KHUtil.GetSFJV(dataObject, "ivrName");
            String action = KHUtil.GetSFJV(dataObject, "action");
            String startTime = KHUtil.GetSFJV(dataObject, "startTime");
            String endTime = KHUtil.GetSFJV(dataObject, "endTime");
            String pressKey = KHUtil.GetSFJV(dataObject, "pressKey");
            String pressTime = KHUtil.GetSFJV(dataObject, "pressTime");

            sql = '''Insert Into Clink_Describe_Cdr_Ib_Ivr
                      (Uniqueid,
                       Pathname,
                       Path,
                       Routerstarttime,
                       Routerendtime,
                       Routername,
                       Ivrname,
                       Action,
                       Starttime,
                       Endtime,
                       Presskey,
                       Presstime)
                    Values
                      (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''';

            jt.update(sql, uniqueId, pathName, path, routerStartTime, routerEndTime, routerName, ivrName, action, startTime, endTime, pressKey, pressTime);

        }

        LogToDB.toSyncFile_Log_List(eliteBean, Oper_No, uniqueId, "2-1-呼入通话记录明细-IVR", "2-1-呼入通话记录明细-IVR Data Result size:" + dataArrayLen);

    }

    // 3-呼出通话记录
    static String toDoOBGet(EliteBean eliteBean, JdbcTemplate jt, String StartTime, String Offset) {

        //处理主数据
        log.debug("3-呼出通话记录 toDoGet Data StartTime:" + StartTime + " Offset:" + Offset);

        long startTime_Loop = System.currentTimeMillis();

        // 系统接入-GET
        String getPath = "/cc/copy_cdr_obs?";

        int totalNum = 0;
        int loopLimit = 0;
        while (!Offset.equals("noScrollId") && loopLimit < 1000) {

            Map<String, String> queryParams = new HashMap<String, String>();
            queryParams.put("hiddenType", "0");
            queryParams.put("date", StartTime);

            if (!KHUtil.isNull(Offset)) {
                queryParams.put("scrollId", Offset);
            }
            queryParams.put("limit", "100");
            log.debug("系统接入 queryParams:" + queryParams);

            String doGETRes = ClinkUtils.doClinkCall("GET", getPath, queryParams, headMap, null);
            log.debug("系统接入 doGETRes:" + doGETRes);

            JSONObject doGETJSON = JSONObject.fromObject(doGETRes);
            // log.debug("3-呼出通话记录 Data Result:" + doGETJSON);

            JSONArray dataArray = doGETJSON.getJSONArray("cdrObs"); ;
            int dataArrayLen = dataArray.size();
            totalNum = totalNum + dataArrayLen;
            log.debug("3-呼出通话记录 Data Result size:" + dataArrayLen);

            JSONObject dataObject = null;
            String sql = null;
            List sqlResult = null;
            int Countnum = 0;
            for (int i = 0; i < dataArrayLen; i++) {
                dataObject = dataArray.getJSONObject(i);
                String callId = KHUtil.GetSFJV(dataObject, "callId");
                String uniqueId = KHUtil.GetSFJV(dataObject, "uniqueId");
                String hotline = KHUtil.GetSFJV(dataObject, "hotline");
                String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
                String customerNumberEncrypt = KHUtil.GetSFJV(dataObject, "customerNumberEncrypt");
                String customerProvince = KHUtil.GetSFJV(dataObject, "customerProvince");
                String customerCity = KHUtil.GetSFJV(dataObject, "customerCity");
                String cno = KHUtil.GetSFJV(dataObject, "cno");
                String clientName = KHUtil.GetSFJV(dataObject, "clientName");
                String clientNumber = KHUtil.GetSFJV(dataObject, "clientNumber");
                String callType = KHUtil.GetSFJV(dataObject, "callType");
                String startTime = KHUtil.GetSFJV(dataObject, "startTime");
                String endTime = KHUtil.GetSFJV(dataObject, "endTime");
                String bridgeTime = KHUtil.GetSFJV(dataObject, "bridgeTime");
                String bridgeDuration = KHUtil.GetSFJV(dataObject, "bridgeDuration");
                String totalDuration = KHUtil.GetSFJV(dataObject, "totalDuration");
                String ivrName = KHUtil.GetSFJV(dataObject, "ivrName");
                String status = KHUtil.GetSFJV(dataObject, "status");
                String statusCode = KHUtil.GetSFJV(dataObject, "statusCode");
                String endReason = KHUtil.GetSFJV(dataObject, "endReason");
                String sipCause = KHUtil.GetSFJV(dataObject, "sipCause");
                String recordFile = KHUtil.GetSFJV(dataObject, "recordFile");
                String userField = KHUtil.GetSFJV(dataObject, "userField");
                String xnumber = KHUtil.GetSFJV(dataObject, "xnumber");
                String mark = KHUtil.GetSFJV(dataObject, "mark");
                String tags = KHUtil.GetSFJV(dataObject, "tags");
                String qno = KHUtil.GetSFJV(dataObject, "qno");
                String qname = KHUtil.GetSFJV(dataObject, "qname");
                String markData = KHUtil.GetSFJV(dataObject, "markData");
                String customerRingingTime = KHUtil.GetSFJV(dataObject, "customerRingingTime");
                String queueAnswerInTime = KHUtil.GetSFJV(dataObject, "queueAnswerInTime");
                String evaluation = KHUtil.GetSFJV(dataObject, "evaluation");
                String businessIds = KHUtil.GetSFJV(dataObject, "businessIds");
                String ticketIds = KHUtil.GetSFJV(dataObject, "ticketIds");

                sql = '''replace INTO clink_list_cdr_obs (callId, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, cno, clientName, clientNumber, callType, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, xnumber, mark, tags, qno, qname, markData, customerRingingTime, queueAnswerInTime, evaluation, businessIds, ticketIds) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''';

                jt.update(sql, callId, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, cno, clientName, clientNumber, callType, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, xnumber, mark, tags, qno, qname, markData, customerRingingTime, queueAnswerInTime, evaluation, businessIds, ticketIds);

            }

            loopLimit++;

            Offset = KHUtil.GetSFJV(doGETJSON, "scrollId");
            Offset = KHUtil.isNull(Offset) ? "noScrollId" : Offset;

        }

        LogToDB.toSyncFile_Log(eliteBean, Oper_No + KHUtil.getIntRandom(4), "3-呼出通话记录", "apiClinkCdrCopyRecord", "toDoOBGet", getPath, String.valueOf(loopLimit), String.valueOf(totalNum), "获取次数:" + loopLimit + " 总数量:" + totalNum, String.valueOf(System.currentTimeMillis() - startTime_Loop));

        return "获取次数:" + loopLimit + " 总数量:" + totalNum;

    }

    // 4-呼出通话记录明细
    static String toDoOBGetd(EliteBean eliteBean, JdbcTemplate jt, String StartTime, String Offset) {

        //处理主数据
        log.debug("4-呼出通话记录明细 toDoGet Data StartTime:" + StartTime + " Offset:" + Offset);

        long startTime_Loop = System.currentTimeMillis();

        // 系统接入-GET
        String getPath = "/cc/copy_cdr_obs?";

        int totalNum = 0;
        int loopLimit = 0;
        while (!Offset.equals("noScrollId") && loopLimit < 1000) {

            Map<String, String> queryParams = new HashMap<String, String>();
            queryParams.put("hiddenType", "0");
            queryParams.put("date", StartTime);

            if (!KHUtil.isNull(Offset)) {
                queryParams.put("scrollId", Offset);
            }
            queryParams.put("limit", "100");
            log.debug("系统接入 queryParams:" + queryParams);

            String doGETRes = ClinkUtils.doClinkCall("GET", getPath, queryParams, headMap, null);
            log.debug("系统接入 doGETRes:" + doGETRes);

            JSONObject doGETJSON = JSONObject.fromObject(doGETRes);
            // log.debug("4-呼出通话记录明细 Data Result:" + doGETJSON);

            JSONArray dataArray = doGETJSON.getJSONArray("cdrObs"); ;
            int dataArrayLen = dataArray.size();
            totalNum = totalNum + dataArrayLen;
            log.debug("4-呼出通话记录明细 Data Result size:" + dataArrayLen);

            JSONObject dataObject = null;
            String sql = null;
            List sqlResult = null;
            int Countnum = 0;
            for (int i = 0; i < dataArrayLen; i++) {
                dataObject = dataArray.getJSONObject(i);

                String uniqueId = KHUtil.GetSFJV(dataObject, "uniqueId");
                String callId = KHUtil.GetSFJV(dataObject, "callId");
                String hotline = KHUtil.GetSFJV(dataObject, "hotline");
                String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
                String customerNumberEncrypt = KHUtil.GetSFJV(dataObject, "customerNumberEncrypt");
                String customerProvince = KHUtil.GetSFJV(dataObject, "customerProvince");
                String customerCity = KHUtil.GetSFJV(dataObject, "customerCity");
                String cno = KHUtil.GetSFJV(dataObject, "cno");
                String clientName = KHUtil.GetSFJV(dataObject, "clientName");
                String clientNumber = KHUtil.GetSFJV(dataObject, "clientNumber");
                String callType = KHUtil.GetSFJV(dataObject, "callType");
                String startTime = KHUtil.GetSFJV(dataObject, "startTime");
                String endTime = KHUtil.GetSFJV(dataObject, "endTime");
                String bridgeTime = KHUtil.GetSFJV(dataObject, "bridgeTime");
                String bridgeDuration = KHUtil.GetSFJV(dataObject, "bridgeDuration");
                String totalDuration = KHUtil.GetSFJV(dataObject, "totalDuration");
                String ivrName = KHUtil.GetSFJV(dataObject, "ivrName");
                String status = KHUtil.GetSFJV(dataObject, "status");
                String statusCode = KHUtil.GetSFJV(dataObject, "statusCode");
                String endReason = KHUtil.GetSFJV(dataObject, "endReason");
                String sipCause = KHUtil.GetSFJV(dataObject, "sipCause");
                String recordFile = KHUtil.GetSFJV(dataObject, "recordFile");
                String userField = KHUtil.GetSFJV(dataObject, "userField");
                String xnumber = KHUtil.GetSFJV(dataObject, "xnumber");
                String investigation = KHUtil.GetSFJV(dataObject, "investigation");
                String tags = KHUtil.GetSFJV(dataObject, "tags");
                String mark = KHUtil.GetSFJV(dataObject, "mark");
                String qno = KHUtil.GetSFJV(dataObject, "qno");
                String qname = KHUtil.GetSFJV(dataObject, "qname");
                String markData = KHUtil.GetSFJV(dataObject, "markData");
                String customerRingingTime = KHUtil.GetSFJV(dataObject, "customerRingingTime");
                String queueAnswerInTime = KHUtil.GetSFJV(dataObject, "queueAnswerInTime");
                String evaluation = KHUtil.GetSFJV(dataObject, "evaluation");
                // log.debug("4-呼出通话记录明细 Deal Data uniqueId:" + uniqueId);

                sql = '''replace INTO clink_describe_cdr_ob (callId, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, cno, clientName, clientNumber, callType, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, xnumber, investigation, tags, mark, qno, qname, markData, customerRingingTime, queueAnswerInTime, evaluation) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''';

                jt.update(sql, callId, uniqueId, hotline, customerNumber, customerNumberEncrypt, customerProvince, customerCity, cno, clientName, clientNumber, callType, startTime, endTime, bridgeTime, bridgeDuration, totalDuration, ivrName, status, statusCode, endReason, sipCause, recordFile, userField, xnumber, investigation, tags, mark, qno, qname, markData, customerRingingTime, queueAnswerInTime, evaluation);

            }

            loopLimit++;

            Offset = KHUtil.GetSFJV(doGETJSON, "scrollId");
            Offset = KHUtil.isNull(Offset) ? "noScrollId" : Offset;

        }

        LogToDB.toSyncFile_Log(eliteBean, Oper_No + KHUtil.getIntRandom(4), "4-呼出通话记录明细", "apiClinkCdrCopyRecord", "toDoOBGetd", getPath, String.valueOf(loopLimit), String.valueOf(totalNum), "获取次数:" + loopLimit + " 总数量:" + totalNum, String.valueOf(System.currentTimeMillis() - startTime_Loop));

        return "获取次数:" + loopLimit + " 总数量:" + totalNum;

    }


    // 重复来电数据处理
    public static void callRepeatFix(JdbcTemplate jt, String UniqueID) {

        try {
            log.debug("重复来电数据处理 UniqueID:" + UniqueID);

            // 处理主数据
            String dealSQL = '''Update Clink_List_Cdr_Ibs R1,
       (Select R1.Uniqueid,
               R1.Customernumber,
               R1.Starttime,
               Max(If(R4.Qno != '' And R4.Qno Is Not Null And R4.clientRingingTime != '' And R4.status = '座席未接听' And R4.Starttime > R1.Starttime - 1 * 60 * 60, R4.Is_Call_Fail, 0)) As Is_Call_Fail,
               Max(If(R4.Qno != '' And R4.Qno Is Not Null And R4.clientRingingTime='' And R4.mark != 11 And R4.Starttime > R1.Starttime - 1 * 60 * 60, R4.Is_TimeOut_Fail, 0)) As Is_TimeOut_Fail,
               Max(If(R4.Qno != '' And R4.Qno Is Not Null And R4.clientRingingTime='' And R4.mark = 11 And R4.Starttime > R1.Starttime - 1 * 60 * 60, R4.Is_Abandon_Fail, 0)) As Is_Abandon_Fail,
               Max(R2.Starttime) As Last_Call_Time,
               Max(Ifnull(R2.Starttime, '')) As Is_24flag,
               Max(Ifnull(R3.Starttime, '')) As Is_48flag
          From Clink_List_Cdr_Ibs R1
          Left Join Clink_List_Cdr_Ibs R2
            On R1.Customernumber = R2.Customernumber
           And R1.Starttime > R2.Starttime
           And R2.Starttime > R1.Starttime - 24 * 60 * 60
          Left Join Clink_List_Cdr_Ibs R3
            On R1.Customernumber = R3.Customernumber
           And R1.Starttime > R3.Starttime
           And R3.Starttime > R1.Starttime - 48 * 60 * 60
          Left Join Clink_List_Cdr_Ibs R4
            On R1.Customernumber = R4.Customernumber
           And R1.Starttime > R4.Starttime
         Where 1 = 1
           And R1.Uniqueid = ?
         Group By R1.Uniqueid, R1.Customernumber, R1.Starttime) R2
   Set R1.Is_Call_Fail   = If(R2.Is_Abandon_Fail <= 0 And R2.Is_TimeOut_Fail <= 0 And R2.Is_Call_Fail <= 0 And R1.Qno != '' And R1.Qno Is Not Null And R1.Clientringingtime Is Not Null And R1.Status = '座席未接听',
                              1,
                              0),
       R1.Is_TimeOut_Fail   = If(R2.Is_Abandon_Fail <= 0 And R2.Is_TimeOut_Fail <= 0 And R2.Is_Call_Fail <= 0 And R1.Qno != '' And R1.Qno Is Not Null And R1.clientRingingTime='' And R1.mark != 11,
                              1,
                              0),
       R1.Is_Abandon_Fail   = If(R2.Is_Abandon_Fail <= 0 And R2.Is_TimeOut_Fail <= 0 And R2.Is_Call_Fail <= 0 And R1.Qno != '' And R1.Qno Is Not Null And R1.clientRingingTime='' And R1.mark = 11,
                              1,
                              0),
       R1.Last_Call_Time = From_Unixtime(R2.Last_Call_Time),
       R1.Is_24repeat    = If(R2.Is_24flag Is Not Null And R2.Is_24flag != '', 1, 0),
       R1.Is_48repeat    = If(R2.Is_48flag Is Not Null And R2.Is_48flag != '', 1, 0),
       R1.Is_Ignore_Flag   = If((R2.Is_TimeOut_Fail > 0 Or R2.Is_Abandon_Fail > 0 or R2.Is_Call_Fail > 0) And ((R1.Qno != '' And R1.Qno Is Not Null And R1.clientRingingTime='' And (R1.mark != 11 Or R1.mark = 11)) or (R1.Qno != '' And R1.Qno Is Not Null And R1.Clientringingtime Is Not Null And R1.Status = '座席未接听')), 1, 0)
 Where R1.Uniqueid = R2.Uniqueid
   And R1.Uniqueid = ?''';
            int updateNum = jt.update(dealSQL, UniqueID, UniqueID);

            log.debug("重复来电数据处理(" + UniqueID + ") 处理数量：" + updateNum);

        } catch (Exception e) {
            log.error("重复来电数据处理错误", e);
        }

    }

    static String stampToDate(String s) {
        String res = s.length() > 10 ? s : s + "000";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        long lt = new Long(res);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

}